﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using DevExpress.XtraEditors;

using QLTTAN.Form;
using QLTTAN.BLL;
using QLTTAN.DTO;
namespace QLTTAN.UserControls
{
    public partial class ucDanhSachPhongThiCuoiKhoa : UserControl
    {
        LichThiBLL LT_BLL;
        KhoaHocBLL KH_BLL;

        int maxKhoaHoc;

        public ucDanhSachPhongThiCuoiKhoa()
        {
            InitializeComponent();
        }

        private void FillDataInLookUpEditKhoaHoc()
        {
            DataTable dt = KH_BLL.KhoaHoc_Load();
            lookUpEditKhoaHoc.Properties.DataSource = dt;
            lookUpEditKhoaHoc.Properties.ValueMember = "MaKhoaHoc";
            lookUpEditKhoaHoc.Properties.DisplayMember = "TenKhoaHoc";

            maxKhoaHoc = dt.Rows.Count - 1;

            lookUpEditKhoaHoc.ItemIndex = maxKhoaHoc;
        }

        private void LoadGridCtrlPhongThiCuoiKhoa()
        {
            if (lookUpEditKhoaHoc.EditValue != null)
            {
                try
                {
                    LopHocDTO LH_DTO = new LopHocDTO();
                    LH_DTO.MaKhoaHoc = int.Parse(lookUpEditKhoaHoc.EditValue.ToString());

                    gridCtrlPhongThiCuoiKhoa.DataSource = LT_BLL.LichThi_Load_MaKhoa(LH_DTO);

                    if (lookUpEditKhoaHoc.ItemIndex == maxKhoaHoc)
                    {
                        btnThem.Enabled = true;
                        btnXoa.Enabled = true;
                    }
                    else
                    {
                        btnThem.Enabled = false;
                        btnXoa.Enabled = false;
                    }
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message, "Thông báo!!!", MessageBoxButtons.OK);
                }
            }
        }

        private void ucDanhSachPhongThiCuoiKhoa_Load(object sender, EventArgs e)
        {
            LT_BLL = new LichThiBLL();
            KH_BLL = new KhoaHocBLL();

            FillDataInLookUpEditKhoaHoc();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (lookUpEditKhoaHoc.EditValue == null)
            {
                XtraMessageBox.Show("Bạn chưa chọn khóa học", "Thông báo!!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            frmLichThiCuoiKhoa frmLTCK = new frmLichThiCuoiKhoa();
            frmLTCK.MaKhoaSelected = int.Parse(lookUpEditKhoaHoc.EditValue.ToString());
            frmLTCK.ShowDialog();

            LoadGridCtrlPhongThiCuoiKhoa();
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            if (gridViewPhongThiCuoiKhoa.FocusedRowHandle < 0)
            {
                XtraMessageBox.Show("Bạn chưa chọn phòng thi để xem", "Thông báo!!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            frmLichThiCuoiKhoa frmLTCK = new frmLichThiCuoiKhoa();
            frmLTCK.MaLichThiSelected = int.Parse(gridViewPhongThiCuoiKhoa.GetDataRow(gridViewPhongThiCuoiKhoa.FocusedRowHandle)["MaLichThi"].ToString());
            frmLTCK.MaKhoaSelected = int.Parse(lookUpEditKhoaHoc.EditValue.ToString());
            frmLTCK.ShowDialog();

            LoadGridCtrlPhongThiCuoiKhoa();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (gridViewPhongThiCuoiKhoa.FocusedRowHandle < 0)
            {
                XtraMessageBox.Show("Bạn chưa chọn phòng thi để xóa", "Thông báo!!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            try
            {
                LichThiDTO LT_DTO = new LichThiDTO();
                LT_DTO.MaLichThi = int.Parse(gridViewPhongThiCuoiKhoa.GetDataRow(gridViewPhongThiCuoiKhoa.FocusedRowHandle)["MaLichThi"].ToString());

                if (LT_BLL.LichThi_Delete(LT_DTO) > 0)
                    XtraMessageBox.Show("Xóa thành công.", "Thông báo!!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    XtraMessageBox.Show("Xóa thất bại.", "Thông báo!!!", MessageBoxButtons.OK, MessageBoxIcon.Information);

                LoadGridCtrlPhongThiCuoiKhoa();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông báo!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnKetQuaThiCuoiKhoa_Click(object sender, EventArgs e)
        {
            if (gridViewPhongThiCuoiKhoa.FocusedRowHandle < 0)
            {
                XtraMessageBox.Show("Bạn chưa chọn phòng thi để xem kết quả", "Thông báo!!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            frmKetQuaThiCuoiKhoa frmKQTCK = new frmKetQuaThiCuoiKhoa();
            frmKQTCK.MaLichThiSelected = int.Parse(gridViewPhongThiCuoiKhoa.GetDataRow(gridViewPhongThiCuoiKhoa.FocusedRowHandle)["MaLichThi"].ToString());
            frmKQTCK.ShowDialog();
        }

        private void lookUpEditKhoaHoc_EditValueChanged(object sender, EventArgs e)
        {
            LoadGridCtrlPhongThiCuoiKhoa();
        }

        private void gridViewPhongThiCuoiKhoa_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "STT")
                e.DisplayText = (e.ListSourceRowIndex + 1).ToString();
        }
    }
}
